<script lang="ts" setup>
import useStore from '@/store/index'
import { ref, watchEffect } from 'vue';
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/message';
import { storeToRefs } from 'pinia';
import { CartItem } from '@/types/cart';
const { goods, cart } = useStore()
const {info} = storeToRefs(goods)
const route = useRoute()
watchEffect(() => {
  // 清空pinia缓存问题
  const id = route.params.id as string
  if (route.fullPath === `/goods/${id}`) {
    goods.resetGoodsInfo()
    goods.getGoodsInfo(id)
  }
})
// 保存skuId
const currentSkuId = ref('')
const changeSku = (skuId: string) => {
  const sku = goods.info.skus.find(item => item.id === skuId)
  if (sku) {
    goods.info.inventory = sku.inventory
    goods.info.price = sku.price
    goods.info.oldPrice = sku.oldPrice
  }
  currentSkuId.value = skuId
}
const count = ref(1)
const addCart = async () => {
  if (!currentSkuId.value) {
    return Message.warning('请选择完整的商品信息')
  }
  // 当前需要添加的商品的sku
  const sku = info.value.skus.find((item) => item.id === currentSkuId.value)!
  const attrsText = sku.specs.map((item) => item.name + ':' + item.valueName).join(' ')
  cart.addCart({
    // 本地添加
    id: info.value.id,
    name: info.value.name,
    picture: info.value.mainPictures[0],
    price: info.value.price,
    count: count.value,
    skuId: currentSkuId.value,
    attrsText: attrsText,
    selected: true,
    nowPrice: info.value.price,
    stock: info.value.inventory,
    isEffective: true,
  } as CartItem)

  Message.success('添加成功')
}
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <template v-if="goods.info.categories">
        <!-- 面包屑 -->
        <XtxBread>
          <XtxBreadItem to="/">首页</XtxBreadItem>
          <XtxBreadItem :to="`/category/${goods.info.categories[1].id}`">{{ goods.info.categories[1].name }}
          </XtxBreadItem>
          <XtxBreadItem :to="`/category/sub/${goods.info.categories[0].id}`">{{ goods.info.categories[0].name }}
          </XtxBreadItem>
          <XtxBreadItem>{{ goods.info.name }}</XtxBreadItem>
        </XtxBread>
        <!-- 商品信息 -->
        <div class="goods-info">
          <div class="media">
            <GoodsImage :images="goods.info.mainPictures" />
            <GoodsSales />
          </div>
          <div class="spec">
            <GoodsName :goods="goods.info" />
            <!-- 父传子 传入skuId -->
            <GoodsSku :goods="goods.info" skuId="1369155864430120962" @change-sku="changeSku" />
            <XtxNumbox :max="goods.info.inventory" showLabel v-model="count">数量</XtxNumbox>
            <XtxButton type="primary" style="margin-top: 20px" @click="addCart">
              加入购物车
            </XtxButton>
          </div>
        </div>
        <!-- 商品详情 -->
        <div class="goods-footer">
          <div class="goods-article">
            <!-- 商品+评价 -->
            <div class="goods-tabs">
              <!-- 商品详情 -->
              <GoodsDetail :goods="goods.info" />
            </div>
          </div>
          <!-- 24热榜+专题推荐 -->
          <!-- 24热榜+专题推荐 -->
          <div class="goods-aside">
            <!-- 24热榜+专题推荐 -->
            <div class="goods-aside">
              <GoodsHot :type="1" />
              <GoodsHot :type="2" />
              <GoodsHot :type="3" />
            </div>
          </div>
        </div>
      </template>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>
